package com.yz.jdd.module.main.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.ItemOnClick;
import com.yz.jdd.network.response.Response_CashierOrderList;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 收银订单列表
 */
public class CashierOrdersAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<Response_CashierOrderList.PageDataBean> responseOrderLists;
    private ItemOnClick itemOnClick;

    public void setItemOnClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }


    public CashierOrdersAdapter(Context context, List<Response_CashierOrderList.PageDataBean> orderListItemList) {
        this.context = context;
        this.responseOrderLists = orderListItemList;
    }

    @Override
    public int getGroupCount() {
        return responseOrderLists.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return responseOrderLists.get(i).getOrderList() != null ? responseOrderLists.get(i).getOrderList().size() : 0;
    }

    @Override
    public Object getGroup(int i) {
        return responseOrderLists.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return responseOrderLists.get(i).getOrderList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cashier_order_list_partent, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.receiptCnt_tv = (TextView) convertView.findViewById(R.id.receiptCnt_tv);
            groupViewHolder.receiptAmt_tv = (TextView) convertView.findViewById(R.id.receiptAmt_tv);
            groupViewHolder.refundCnt_tv = (TextView) convertView.findViewById(R.id.refundCnt_tv);
            groupViewHolder.refundAmt_tv = (TextView) convertView.findViewById(R.id.refundAmt_tv);
            groupViewHolder.date_tv = (TextView) convertView.findViewById(R.id.date_tv);

            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        String dateStr = responseOrderLists.get(i).getDate();
        //获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-d");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        String currDateStr = simpleDateFormat.format(date);
        //  LogUtils.i("返回的日期: " + dateStr + "  / Date获取当前日期时间" + currDateStr);
        if (dateStr != null && dateStr.equals(currDateStr)) {
            groupViewHolder.date_tv.setText(dateStr + " 今天");
        } else {
            groupViewHolder.date_tv.setText(dateStr);
        }

        Drawable upDraw = context.getResources().getDrawable(R.mipmap.order_up);
        Drawable downDraw = context.getResources().getDrawable(R.mipmap.order_down);
        if (b) {
            groupViewHolder.date_tv.setCompoundDrawablesWithIntrinsicBounds(null, null, upDraw, null);
        } else {
            groupViewHolder.date_tv.setCompoundDrawablesWithIntrinsicBounds(null, null, downDraw, null);
        }
        groupViewHolder.receiptCnt_tv.setText(responseOrderLists.get(i).getReceiptCnt());
        groupViewHolder.receiptAmt_tv.setText(responseOrderLists.get(i).getReceiptAmt());
        groupViewHolder.refundCnt_tv.setText(responseOrderLists.get(i).getRefundCnt());
        groupViewHolder.refundAmt_tv.setText(responseOrderLists.get(i).getRefundAmt());

        return convertView;

    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_list, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.paytype_image = (ImageView) convertView.findViewById(R.id.paytype_image);
            childViewHolder.shop_name_tv = (TextView) convertView.findViewById(R.id.shop_name_tv);
            childViewHolder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
            childViewHolder.money_tv = (TextView) convertView.findViewById(R.id.money_tv);
            childViewHolder.state_tv = (TextView) convertView.findViewById(R.id.state_tv);
            childViewHolder.line = (View) convertView.findViewById(R.id.line);
            childViewHolder.load_more = (LinearLayout) convertView.findViewById(R.id.load_more);
            childViewHolder.othr_layout = (LinearLayout) convertView.findViewById(R.id.othr_layout);
            childViewHolder.line_top = (View) convertView.findViewById(R.id.line_top);
            convertView.setTag(childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (responseOrderLists.size() == 1 && i1 == 0) {
            childViewHolder.line_top.setVisibility(View.VISIBLE);
        } else {
            childViewHolder.line_top.setVisibility(View.GONE);
        }
        childViewHolder.shop_name_tv.setText(responseOrderLists.get(i).getOrderList().get(i1).getShopName());
        childViewHolder.time_tv.setText(responseOrderLists.get(i).getOrderList().get(i1).getTradeDate());


        // String orderStatus = "";  // tradeType   交易类型 0:支付交易 1:退款交易
        String encourageAmount;
        String money = "";
        switch (responseOrderLists.get(i).getOrderList().get(i1).getTradeType()) {
            case "0":
                //        orderStatus = "已收款";
                money = responseOrderLists.get(i).getOrderList().get(i1).getPayAmount();
                if (!StringUtil.isEmpty(money)) {
                    childViewHolder.money_tv.setText("+" + money); //"¥ "
                    childViewHolder.money_tv.setTextColor(Color.parseColor("#333333"));
                }
                encourageAmount = responseOrderLists.get(i).getOrderList().get(i1).getEncourageDeductionAmount();
                if (!StringUtil.isEmpty(encourageAmount)) {
                    childViewHolder.state_tv.setVisibility(View.VISIBLE);
                    childViewHolder.state_tv.setText("含津贴 " + encourageAmount);
                }else{
                    childViewHolder.state_tv.setVisibility(View.GONE);
                }
                break;
            case "1":
                //      orderStatus = "已退款";
                money = responseOrderLists.get(i).getOrderList().get(i1).getRefundAmount();
                if (!StringUtil.isEmpty(money)) {
                    childViewHolder.money_tv.setText("-" + money); // "- ¥ "
                    childViewHolder.money_tv.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                }
                encourageAmount = responseOrderLists.get(i).getOrderList().get(i1).getEncourageRefundAmount();
                if (!StringUtil.isEmpty(encourageAmount)) {
                    childViewHolder.state_tv.setVisibility(View.VISIBLE);
                    childViewHolder.state_tv.setText("含津贴 " + encourageAmount);
                }else{
                    childViewHolder.state_tv.setVisibility(View.GONE);
                }
                break;
        }
//        childViewHolder.state_tv.setText(orderStatus);
        if (responseOrderLists.get(i).getOrderList().size() - 1 == i1) {
            childViewHolder.line.setVisibility(View.GONE);
        } else {
            childViewHolder.line.setVisibility(View.VISIBLE);
        }

//        支付方式payType
        switch (responseOrderLists.get(i).getOrderList().get(i1).getPayType()) {
            case "2": // type="2": 支付宝
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.zfb);
                break;
            case "5"://   type="5":微信
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.wx);
                break;
            case "3"://   type="3":会员卡
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.pay_vip);
                break;
            case "4"://    type="4":银联
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.ylsk);
                break;
            case "1"://    type="1":现金
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.pay_xianjin);
                break;
            case "0": //   type="0":其他
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.wx);
                break;
            case "7":
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.ysf);
                break;
            case "8": //余额支付
                childViewHolder.paytype_image.setBackgroundResource(R.mipmap.yezf);
                break;
        }
        if (responseOrderLists.get(i).isMore() && b) {
            childViewHolder.load_more.setVisibility(View.VISIBLE);
//            childViewHolder.load_more.setFocusable(true);
        } else {
            childViewHolder.load_more.setVisibility(View.GONE);
        }
        childViewHolder.load_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.i("mytag -------load_more----");
                itemOnClick.layoutOnClick(true, i, i1);
            }
        });
        childViewHolder.othr_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtils.i("mytag ------othr_layout-----");
                itemOnClick.layoutOnClick(false, i, i1);
            }
        });
        return convertView;

    }

    static class GroupViewHolder {
        TextView receiptCnt_tv;
        TextView receiptAmt_tv;
        TextView refundCnt_tv;
        TextView refundAmt_tv;
        TextView date_tv;
    }

    static class ChildViewHolder {
        ImageView paytype_image;
        TextView shop_name_tv;
        TextView time_tv;
        TextView money_tv;
        TextView state_tv;
        LinearLayout load_more;
        LinearLayout othr_layout;
        View line;
        View line_top;

    }
}
